import 'package:flutter/material.dart';
import 'package:flutter_study/config/ConstColor.dart';
import 'package:flutter_study/page/topics_page.dart';

import 'category_page.dart';
import 'follow_page.dart';
import 'news_list_page.dart';
import 'recommend_page.dart';

const TAB_LABEL = ['关注', '分类', '专题', '资讯', '推荐'];

class DiscoveryPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _DiscoveryPageState();
  }
}

class _DiscoveryPageState extends State<DiscoveryPage>
    with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin {
  TabController _tabController;
  PageController _pageController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: TAB_LABEL.length, vsync: this);
    _pageController = PageController();
  }

  @override
  void dispose() {
    _tabController.dispose();
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: AppBar(
        brightness: Brightness.light,
        centerTitle: true,
        elevation: 0,
        backgroundColor: Colors.white,
        title: Text(
          "发现",
          style: TextStyle(
              fontSize: 18, color: Colors.black, fontWeight: FontWeight.bold),
        ),
      ),
      body: Column(
        children: [
          _buildTabBarView(),
          _buildPageView(),
        ],
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;

  _buildTabBarView() {
    return Container(
      decoration: BoxDecoration(color: Colors.white),
      child: TabBar(
        controller: _tabController,
        labelColor: Colors.black,
        unselectedLabelColor: ConstColor.hitTextColor,
        labelStyle: TextStyle(fontSize: 14),
        indicatorColor: Colors.black,
        indicatorSize: TabBarIndicatorSize.label,
        tabs: TAB_LABEL.map((e) => Tab(text: e)).toList(),
        onTap: (index) {
          _pageController.animateToPage(index,
              duration: kTabScrollDuration, curve: Curves.ease);
        },
      ),
    );
  }

  _buildPageView() {
    return Expanded(
      child: PageView(
        controller: _pageController,
        onPageChanged: (index) {
          _tabController.index = index;
        },
        children: [
          FollowPage(),
          CategoryPage(),
          TopicsPage(),
          NewsListPage(),
          RecommendPage()
        ],
      ),
    );
  }
}
